using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class ItemManager : MonoBehaviour
{
	public ItemData[] ItemsList;

	public Dictionary<int, FPSItemEquipment> ItemEquipments = new Dictionary<int, FPSItemEquipment>();

	public string Suffix = "UZ";

	public int itemCount;

	private void Start()
	{
		for (int i = 0; i < ItemsList.Length; i++)
		{
			UnitZ.gameNetwork.spawnPrefabs.Add(ItemsList[i].gameObject);
			if (!ItemsList[i].ItemFPS)
			{
				continue;
			}
			FPSItemPlacing component = ItemsList[i].ItemFPS.GetComponent<FPSItemPlacing>();
			if ((bool)component && component.Item != null)
			{
				NetworkIdentity component2 = component.Item.GetComponent<NetworkIdentity>();
				if ((bool)component2)
				{
					UnitZ.gameNetwork.spawnPrefabs.Add(component.Item.gameObject);
				}
			}
			FPSItemThrow component3 = ItemsList[i].ItemFPS.GetComponent<FPSItemThrow>();
			if ((bool)component3 && component3.Item != null)
			{
				NetworkIdentity component4 = component3.Item.GetComponent<NetworkIdentity>();
				if ((bool)component4)
				{
					UnitZ.gameNetwork.spawnPrefabs.Add(component3.Item.gameObject);
				}
			}
		}
	}

	private void Awake()
	{
		ItemEquipments = new Dictionary<int, FPSItemEquipment>(ItemsList.Length);
		for (int i = 0; i < ItemsList.Length; i++)
		{
			ItemsList[i].ItemID = Suffix + i;
			ItemsList[i].ItemIndex = i;
			if (!ItemsList[i].ItemFPS)
			{
				continue;
			}
			FPSItemEquipment component = ItemsList[i].ItemFPS.GetComponent<FPSItemEquipment>();
			if ((bool)component)
			{
				component.ItemID = ItemsList[i].ItemID;
				component.ItemIndex = i;
				ItemEquipments[i] = component;
			}
			FPSWeaponEquipment component2 = ItemsList[i].ItemFPS.GetComponent<FPSWeaponEquipment>();
			if ((bool)component2 && (bool)ItemsList[i].ItemEquip)
			{
				ItemsList[i].ItemEquip.UsingType = component2.UsingType;
			}
			FPSItemPlacing component3 = ItemsList[i].ItemFPS.GetComponent<FPSItemPlacing>();
			if (!component3 || !(component3.Item != null))
			{
				continue;
			}
			ObjectSpawn component4 = component3.Item.GetComponent<ObjectSpawn>();
			if (!component4)
			{
				continue;
			}
			component4.ItemID = ItemsList[i].ItemID;
			if ((bool)component4.Item)
			{
				ObjectPlacing component5 = component4.Item.GetComponent<ObjectPlacing>();
				if ((bool)component5)
				{
					component5.NetworkItemID = ItemsList[i].ItemID;
				}
			}
		}
	}

	public FPSItemEquipment GetFPSitem(int index)
	{
		return ItemEquipments[index];
	}

	public ItemData GetItem(int index)
	{
		if (index < ItemsList.Length && index >= 0)
		{
			return ItemsList[index];
		}
		return null;
	}

	public int GetIndexByID(string itemid)
	{
		for (int i = 0; i < ItemsList.Length; i++)
		{
			if (itemid == ItemsList[i].ItemID)
			{
				return i;
			}
		}
		return -1;
	}

	public int GetIndexByName(string itemname)
	{
		for (int i = 0; i < ItemsList.Length; i++)
		{
			if (itemname == ItemsList[i].ItemName)
			{
				return i;
			}
		}
		return -1;
	}

	public ItemData CloneItemData(ItemData item)
	{
		for (int i = 0; i < ItemsList.Length; i++)
		{
			if (item.ItemID == ItemsList[i].ItemID)
			{
				return ItemsList[i];
			}
		}
		return null;
	}

	public int GetItemIndexByItemData(ItemData item)
	{
		for (int i = 0; i < ItemsList.Length; i++)
		{
			if (item.ItemID == ItemsList[i].ItemID)
			{
				return i;
			}
		}
		return -1;
	}

	public ItemData CloneItemDataByIndex(string itemID)
	{
		for (int i = 0; i < ItemsList.Length; i++)
		{
			if (ItemsList[i].ItemID == itemID)
			{
				return ItemsList[i];
			}
		}
		return null;
	}

	public ItemData GetItemDataByID(string itemid)
	{
		for (int i = 0; i < ItemsList.Length; i++)
		{
			if (itemid == ItemsList[i].ItemID)
			{
				return ItemsList[i];
			}
		}
		return null;
	}

	public ItemData GetItemDataByName(string itemname)
	{
		for (int i = 0; i < ItemsList.Length; i++)
		{
			if (itemname == ItemsList[i].ItemName)
			{
				return ItemsList[i];
			}
		}
		return null;
	}

	public void PlacingObject(string itemid, Vector3 position, Vector3 normal)
	{
		ItemData itemDataByID = GetItemDataByID(itemid);
		if (!itemDataByID.ItemFPS)
		{
			return;
		}
		FPSItemPlacing component = itemDataByID.ItemFPS.GetComponent<FPSItemPlacing>();
		if ((bool)component && (bool)component.Item)
		{
			GameObject gameObject = UnitZ.gameNetwork.RequestSpawnObject(component.Item, position, Quaternion.identity);
			if ((bool)gameObject)
			{
				ObjectPlacing component2 = gameObject.GetComponent<ObjectPlacing>();
				component2.transform.forward = normal;
				component2.SetItemID(itemid);
			}
		}
	}

	public void DirectPlacingObject(string itemid, string itemuid, Vector3 position, Quaternion rotation)
	{
		ItemData itemDataByID = GetItemDataByID(itemid);
		if (!itemDataByID.ItemFPS)
		{
			return;
		}
		FPSItemPlacing component = itemDataByID.ItemFPS.GetComponent<FPSItemPlacing>();
		if ((bool)component && (bool)component.Item)
		{
			GameObject gameObject = UnitZ.gameNetwork.RequestSpawnObject(component.Item, position, rotation);
			if ((bool)gameObject)
			{
				ObjectPlacing component2 = gameObject.GetComponent<ObjectPlacing>();
				component2.SetItemID(itemid);
				component2.SetItemUID(itemuid);
			}
		}
	}
}
